//package config.db;
//
//import entity.mongo.User;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.event.ContextRefreshedEvent;
//import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.data.mongodb.core.index.IndexOperations;
//import org.springframework.data.mongodb.core.index.IndexResolver;
//import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver;
//import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
//
//public class DataSourceInitListener  implements ApplicationListener<ContextRefreshedEvent> {
//
//    //    protected static final Logger LOGGER = LoggerFactory.getLogger(DataSourceInitListener.class);
//    @Autowired
//    MongoMappingContext mongoMappingContext;
//    @Autowired
//    MongoTemplate mongoTemplate;
//    @SuppressWarnings("unchecked")
//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent ev) {
//        //防止重复执行。
//        if(ev.getApplicationContext().getParent() == null){
//            IndexOperations indexOps = mongoTemplate.indexOps(User.class);
//
//            IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
//            resolver.resolveIndexFor(User.class).forEach(indexOps::ensureIndex);
//
//        }
//    }
//
//}